package com.controller;

import com.entity.Attachment;
import com.service.AttachmentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

@Controller
@RequestMapping("admin/attachment")
public class AttachmentController {
    @Autowired
    private AttachmentService attachmentService;

    private String imgSuffixs = ".png.jpeg.jpg.bmp";

    private String rootpath = "d://files/";

    /**
     * 附件下载
     *
     * @param id 附件id
     */
    @RequestMapping(value = "file/{id}")
    public void download(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Attachment attachment = attachmentService.getModel(id);
        String path = attachment.getPath();
        // 后缀名
        String suffixName = path.substring(path.lastIndexOf("."));
        // 设置强制下载不打开
        response.setContentType("application/force-download;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(attachment.getName(), "UTF-8"));

        String realPath =  rootpath + path;
        try {
            long downloadedLength = 0L;
            //打开本地文件流
            InputStream inputStream = new FileInputStream(realPath);
            //激活下载操作
            OutputStream os = response.getOutputStream();

            //循环写入输出流
            byte[] b = new byte[2048];
            int length;
            while ((length = inputStream.read(b)) > 0) {
                os.write(b, 0, length);
                downloadedLength += b.length;
            }
            // 这里主要关闭。
            os.close();
            inputStream.close();
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 查看PDF
     *
     * @param id 附件id
     */
    @RequestMapping(value = "pdf/{id}")
    public void pdfView(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) throws Exception {
        if (StringUtils.isBlank(id)){
            return;
        }
        Attachment attachment = attachmentService.getModel(id);
        String path = attachment.getPath();
        // 后缀名
        String suffix = attachment.getSuffix();
        String realPath =  rootpath + path;
        try {
            long downloadedLength = 0L;
            //打开本地文件流
            InputStream inputStream;
            inputStream = new FileInputStream(realPath);
            //激活下载操作
            OutputStream os = response.getOutputStream();

            //循环写入输出流
            byte[] b = new byte[2048];
            int length;
            while ((length = inputStream.read(b)) > 0) {
                os.write(b, 0, length);
                downloadedLength += b.length;
            }
            // 这里主要关闭。
            os.close();
            inputStream.close();
        } catch (Exception e) {
            throw e;
        }
    }


    /**
     * 图片查看大图
     *
     * @param id 附件id
     */
    @RequestMapping(value = "image/{id}")
    public void imageView(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) throws Exception {
        if (StringUtils.isBlank(id)){
            return;
        }
        Attachment attachment = attachmentService.getModel(id);
        String path = attachment.getPath();
        // 后缀名
        String suffix = attachment.getSuffix();
        String realPath =  rootpath + path;
        try {
            long downloadedLength = 0L;
            //打开本地文件流
            InputStream inputStream;

            if (StringUtils.isNotBlank(suffix) && "pdf".equals(suffix.toLowerCase())){
                ClassPathResource classPathResource = new ClassPathResource("static/img/pdf.png");
                inputStream = classPathResource.getInputStream();
            } else {
                inputStream = new FileInputStream(realPath);
            }

            //激活下载操作
            OutputStream os = response.getOutputStream();

            //循环写入输出流
            byte[] b = new byte[2048];
            int length;
            while ((length = inputStream.read(b)) > 0) {
                os.write(b, 0, length);
                downloadedLength += b.length;
            }
            // 这里主要关闭。
            os.close();
            inputStream.close();
        } catch (Exception e) {
            throw e;
        }
    }

    //输出视频流
    @RequestMapping(value = "video/{id}")
    public void video(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Attachment attachment = attachmentService.getModel(id);
        String path = attachment.getPath();
        // 后缀名
        String suffixName = path.substring(path.lastIndexOf("."));
        response.addHeader("Content-Type","video/mp4;utf-8");
        String realPath =  rootpath + path;
        try {
            long downloadedLength = 0L;
            //打开本地文件流
            InputStream inputStream = new FileInputStream(realPath);
            //激活下载操作
            OutputStream os = response.getOutputStream();
            //循环写入输出流
            byte[] b = new byte[2048];
            int length;
            while ((length = inputStream.read(b)) > 0) {
                os.write(b, 0, length);
                downloadedLength += b.length;
            }
            // 这里主要关闭。
            os.close();
            inputStream.close();
        } catch (Exception e) {
            throw e;
        }
    }
}
